import ViewUIPlus from "view-ui-plus";
import cookies from "./util.cookies";
import log from "./util.log";
import env from "./util.env";
import db from "./util.db";

import Setting from "@/setting";

const util: Record<string, any> = {
  cookies,
  log,
  env,
  db,
};

function tTitle(title = "") {
  const $t = window ? window["$app"].$t : null;
  if ($t) {
    if (title.indexOf("$t:") === 0) {
      return $t(title.split("$t:")[1]);
    } else {
      return title;
    }
  } else {
    return title;
  }
}

/**
 * @description 更改标题
 * @param {Object} title 标题
 * @param {Object} count 未读消息数提示（可视情况选择使用或不使用）
 */
util.title = function ({ title, count }) {
  title = tTitle(title);
  let fullTitle;

  if (!!Setting.titleSuffix === false || Setting.titleSuffix === "") {
    fullTitle = title ? `${title}` : "";
  } else {
    fullTitle = title
      ? `${title} - ${Setting.titleSuffix}`
      : Setting.titleSuffix;
  }

  if (count) fullTitle = `(${count}条消息)${fullTitle}`;
  window.document.title = fullTitle;
};

//确认弹框
util.confirmTip = function (title, message, CallbackFn) {
  ViewUIPlus.Modal.confirm({
    title: title,
    content: message,
    onOk: () => {
      CallbackFn();
    },
  });
};

/**
 * @description 树结构数据转普通列表
 * @param {Object} tree 后端返回数据
 * @param {Object} map {value: 'code',  label: 'name', children: 'list'}
 */

util.treeListToList = function(tree,parent_id='0'){
  let list = []
  if (!tree || !tree.length) { return;}
   tree.map((item)=>{
     let children = item["children"] ? item["children"] : [];
     item.parentId = parent_id;
     if (children.length > 0) {
      children = util.treeListToList(children,item.id);
     }
    list = [...list,{ ...item }, ...children];
   })
  return list;
}

/**
 * @description 级联菜单数据格式处理
 * @param {Object} tree 后端返回数据
 * @param {Object} map {value: 'code',  label: 'name', children: 'list'}
 */

util.convertCascaderTree = function (tree, map) {
  const result = [];
  tree.forEach((item) => {
    // 读取 map 的键值映射
    let value = item[map.value].toString();
    let label = item[map.label];
  
    let children = item[map.children] ? item[map.children]:[];
    // 如果有子节点，递归
    if (children.length > 0) {
      children = util.convertCascaderTree(children, map);
    }
    result.push({ value, label, ...item, children });
  });
  return result;
};

/**
 * @description tree, treeselect 组件数据格式处理
 * @param {Object} tree 后端返回数据
 * @param {Object} map {title: 'name', children: 'list'}
 */

util.convertTree = function (tree, map, expandNode = false) {
  const result = [];
  tree.forEach((item) => {
    // 读取 map 的键值映射
    let expand = expandNode;
    let title = item[map.title];
    let value = item[map.value];
    let selected = false;
    let checked =false;

    let children = item[map.children] ? item[map.children] : [];
    // 如果有子节点，递归
    if (children) {
      children = util.convertTree(children, map);
    }
    result.push({ expand, value, title, selected, checked, children });
  });
  
  return result;
};

function requestAnimation(task) {
  if ("requestAnimationFrame" in window) {
    return window.requestAnimationFrame(task);
  }

  setTimeout(task, 16);
}

export { requestAnimation };

export default util;
